//
//  HoController.swift
//  ApiManager
//
//  Created by HOA on 2020/5/18.
//

import UIKit

public enum HoControllerNextType {
    
    case present
    
    case push
}

public struct HoController {
    
    static var date: Date!
    
    public static func toNextViewController(_ viewController: UIViewController, nextType: HoControllerNextType = .push, animated: Bool = true) {
        
        let currentVC = current()

        if let date = date, date.timeIntervalSinceNow < 1 {
            self.date = nil
            return
        }
        else {
            date = Date()
        }
        
        switch nextType {
        case .push:
            if let current = currentVC as? UINavigationController {
                current.pushViewController(viewController, animated: animated)
            }
            else if let nav = currentVC.navigationController {
                nav.pushViewController(viewController, animated: animated)
            }
            else {
                fatalError("当前控制器不是导航栏控制器，或者不在导航栏容器内。请使用present方式跳转")
            }
        case .present:
            currentVC.present(viewController, animated: true)
        }
        
        
    }
    
    
    public static func current() -> UIViewController {
        let vc = UIApplication.shared.keyWindow?.rootViewController ?? UIApplication.shared.windows.first?.rootViewController
        return HoController.findBestViewController(vc: vc!)
    }
    
    public static func pushViewController(_ vc: UIViewController, animated: Bool = true) {
        let currentVC = current()
        if let current = currentVC as? UINavigationController {
            current.pushViewController(vc, animated: animated)
        }
        else {
            currentVC.navigationController?.pushViewController(vc, animated: animated)
        }
    }
    
    static func findBestViewController(vc : UIViewController) -> UIViewController {
        
        if vc.presentedViewController != nil {
            return HoController.findBestViewController(vc: vc.presentedViewController!)
        } else if vc.isKind(of:UISplitViewController.self) {
            let svc = vc as! UISplitViewController
            if svc.viewControllers.count > 0 {
                return HoController.findBestViewController(vc: svc.viewControllers.last!)
            } else {
                return vc
            }
        } else if vc.isKind(of: UINavigationController.self) {
            let nvc = vc as! UINavigationController
            if nvc.viewControllers.count > 0 {
                return HoController.findBestViewController(vc: nvc.topViewController!)
            } else {
                return vc
            }
        } else if vc.isKind(of: UITabBarController.self) {
            let tvc = vc as! UITabBarController
            if (tvc.viewControllers?.count)! > 0 {
                return HoController.findBestViewController(vc: tvc.selectedViewController!)
            } else {
                return vc
            }
        } else {
            return vc
        }
    }
    
}
